capture log close
clear all
set more off

* Stablish Working Directory ***************************************************
cd "$workdirectory"

*-------------------------------------------------------------------------------
* Creating shares of subdistricts located within a FUA
import delimited raw_datasets\Maps\intersection_adm1.csv, encoding(UTF-8) clear
save "processed_datasets\dataset_maps_and_cities_adm1_classwk", replace

* Importing area of districts
* We drop observations without geolevel1 and also observations with geolevel == 888888

import delimited raw_datasets\Maps\area_dist.csv, encoding(UTF-8) clear
keep geolevel1 area_dist
drop if geolevel1 == .
drop if geolevel1 == 888888
tempfile area_dist
save `area_dist', replace

* Merging and obtaining shares
use "processed_datasets\dataset_maps_and_cities_adm1_classwk", clear
merge m:1 geolevel1 using `area_dist'
keep if _merge == 3
drop _merge

gen share = area_calcu/area_dist
sort geolevel1
save "processed_datasets\dataset_maps_and_cities_adm1_classwk", replace

*-------------------------------------------------------------------------------
* Generating industry per city data

* Generating empty file to attach data to
clear all
tempfile data_classwk_cities_adm1
save `data_classwk_cities_adm1', empty replace

* Genearting local with all .csv in the folder
local files : dir "raw_datasets\Maps\classwk_maps_adm1" files "*.csv"

cd raw_datasets\Maps\classwk_maps_adm1

* Importing, appending and saving
foreach fi in `files' {
  import delimited `fi', varnames(12) rowrange(13) encoding(UTF-8) clear
  gen country_year = subinstr("`fi'","_classwk.csv","",.)
  append using `data_classwk_cities_adm1'
  save `data_classwk_cities_adm1', replace
}

* Cleaning
keep if v1 == "Weighted N"
drop v1
rename v2 census
drop if census == "COL TOTAL"

* Exctracting geolevel1
gen geolevel1 = regexs(0) if(regexm(census, "[0-9]+"))
destring geolevel1, replace

sort geolevel1
save `data_classwk_cities_adm1', replace


********************************************************************************
cd ..\..\..


* Joining both datasets
use "processed_datasets\dataset_maps_and_cities_adm1_classwk", clear
joinby geolevel1 using `data_classwk_cities_adm1'

* Applying Shares
foreach v of varlist niunotinuniverse selfemployed wagesalaryworker unpaidworker unknownmissing rowtotal other{
	gen _`v' = `v'*share
}

* Fixing country names from both datasets
replace cntry_na_1 = "Lao People's Democratic Republic" if cntry_na_1 == "Laos"

* Drop intersections which associate districts from country i to a city located in country j
drop if cntry_name != cntry_na_1

* Collapsing by city and census
collapse (sum) _* (mean) fua_p_2015 (first) efua_name, by(efua_id country_year)


foreach v2 of varlist _selfemployed _wagesalaryworker _unpaidworker _unknownmissing _other{
	gen share`v2' = `v2'/(_rowtotal-_niunotinuniverse)
}

save "processed_datasets\dataset_maps_and_cities_adm1_classwk", replace

